package gov.pbc.gx.nn.pz.dao;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.bstek.bdf2.core.orm.hibernate.ISessionCallback;

import gov.pbc.gx.nn.pz.entity.Account;
import gov.pbc.gx.nn.pz.entity.HcZwtzpz;
import gov.pbc.gx.nn.pz.entity.HcpzXY;
import gov.pbc.gx.nn.pz.entity.PzFont;

@Repository
public class ZwtzpzDao extends BaseImageDao {

	private Integer PZ_CODE_ = 6;

	public ZwtzpzDao() {
		super();

	}

	public boolean isSameValue(HcZwtzpz hcZwtzpz) {
		String type = hcZwtzpz.getType();
		String pzTemplate = hcZwtzpz.getPzTemplate();
		String hql = " from HcZwtzpz where deptId=:deptid and type=:type and pzTemplate=:pzTemplate and invalid=false ";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("type", type);
		map.put("pzTemplate", pzTemplate);
		List<HcZwtzpz> hcZwtzpzdatas = this.query(hql, map);
		if (!hcZwtzpzdatas.isEmpty()) {
			return true;
		}
		return false;
	}

	public void saveZwtzpz(HcZwtzpz hcZwtzpz) {
		if (!isSameValue(hcZwtzpz)) {
			hcZwtzpz.setId(null);
			hcZwtzpz.setDeptId(this.getDeptId());
			this.save(hcZwtzpz);
		} else {
			this.update(hcZwtzpz);
		}
	}

	public HcZwtzpz selectOneByTypeAndTemplate(String type, String pzTemplate) {
		List<HcZwtzpz> hcZwtzpzs = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		map.put("pzTemplate", pzTemplate);
		if (type == null) {
			String hql = " from HcZwtzpz where deptId=:deptid and type is null and pzTemplate=:pzTemplate and invalid=false";
			hcZwtzpzs = this.query(hql, map);
		} else {
			String hql = " from HcZwtzpz where deptId=:deptid and type=:type and pzTemplate=:pzTemplate and invalid=false";
			map.put("type", type);
			hcZwtzpzs = this.query(hql, map);
		}
		if (!hcZwtzpzs.isEmpty()) {
			HcZwtzpz zwt=hcZwtzpzs.get(0);
			zwt.setPzDate(new Date());
			return zwt;
		}
		return null;
	}

	public Collection<HcZwtzpz> getCoByType(String type) {
		Collection<HcZwtzpz> hcZwtzpzs;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		if (type == null) {
			String hql = " from HcZwtzpz where deptId=:deptid and  invalid = false and type is null";

			hcZwtzpzs = this.query(hql, map);
		} else {
			String hql = " from HcZwtzpz where deptId=:deptid and invalid = false and type=:type";
			map.put("type", type);
			hcZwtzpzs = this.query(hql, map);
			for(HcZwtzpz zwt:hcZwtzpzs){
				zwt.setPzDate(new Date());
			}
		}
		return hcZwtzpzs;
	}

	public Collection<HcZwtzpz> getOneGroupByType() {
		String hql = " from HcZwtzpz where deptId=:deptid and invalid = false group by type";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("deptid", this.getDeptId());
		Collection<HcZwtzpz> hcZwtzpzs = this.query(hql, param);
		for(HcZwtzpz zwt:hcZwtzpzs){
			zwt.setPzDate(new Date());
		}
		return hcZwtzpzs;
	}

	public Collection<HcZwtzpz> getMuByDeptName() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptid", this.getDeptId());
		String hql = "from HcZwtzpz where deptId=:deptid and invalid=false order by pzTemplate";
		Collection<HcZwtzpz> hcZwtzpzs=this.query(hql, map);
		for(HcZwtzpz zwt:hcZwtzpzs){
			zwt.setPzDate(new Date());
		}
		return hcZwtzpzs;	

	}

	public List<HcpzXY> getZwtzpzXYs(Integer pzcode) {
		List<HcpzXY> lists = getHcPzXYsBase(pzcode);
		return lists;
	}

	public void saveZwtzpzXY(Collection<HcpzXY> hcZwtzpzxys) {
		saveXjpzXYBase(hcZwtzpzxys, PZ_CODE_);
	}

	public void updateFont(PzFont pzfont) {
		updateFontBase(pzfont, PZ_CODE_);
	}

	public void deleteTemplateById(String id) {

		this.delete(id, HcZwtzpz.class);
	}
	
	/**
	 * 更新模板名稱
	 * 
	 * @auther jack.zhao
	 * @param templatename
	 * @param id
	 * @return
	 */
	public boolean updateTemplateNameDao(final String templatename,
			final String id) {

		// 根據id得到type
		String sql1 = "from HcZwtzpz where id=:id and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("id", id);
		List<HcZwtzpz> HcZwtzpzs1 = query(sql1, param);
		String typestr = HcZwtzpzs1.get(0).getType();

		// 根據type查找所有的pztemplate是否用相同的，
		String sql = "from HcZwtzpz where  invalid = false and type=:type ";
		Map<String, Object> param1 = new HashMap<String, Object>();
		param1.put("type", typestr);

		Collection<HcZwtzpz> HcZwtzpzs = this.query(sql, param1);
		for (HcZwtzpz hczwtzpz : HcZwtzpzs) {
			if (hczwtzpz.getPzTemplate().equals(templatename)) {
				return false;
			}
		}
		// 更新操作
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			String hql = "update HcZwtzpz set pzTemplate=:pzTemplate  where id=:id ";

			@Override
			public Boolean doInSession(Session session) {
				Query query = session.createQuery(hql);
				query.setString("pzTemplate", templatename);
				query.setString("id", id);
				query.executeUpdate();
				return true;
			}

		});

	}

	/**
	 * 更新模板類型
	 * 
	 * @auther jack.zhao
	 * @param templatetype
	 * @param id
	 * @return
	 */
	public boolean updateTemplateTypeDao(final String templatetype,
			final String id) {
		// 判斷類型是否有相同的

		String sql = "select count(*) from HcZwtzpz where type=:type and invalid = false ";
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("type", templatetype);
		int count = this.queryForInt(sql, param);
		if (count > 0) {
			return false;
		}
		// 更新類型
		return this.doInHibernateSession(new ISessionCallback<Boolean>() {
			@Override
			public Boolean doInSession(Session session) {

				String sql = "from HcZwtzpz where id=:id and invalid = false ";
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("id", id);
				List<HcZwtzpz> HcZwtzpzs = query(sql, param);
				String typestr = HcZwtzpzs.get(0).getType();

				String sql1 = "from HcZwtzpz where type=:type and invalid = false ";
				Map<String, Object> param1 = new HashMap<String, Object>();
				param1.put("type", typestr);
				List<HcZwtzpz> HcZwtzpzs1 = query(sql1, param1);
				for (HcZwtzpz hczwtzpz : HcZwtzpzs1) {
					String hql2 = "update HcZwtzpz set type=:type  where id=:id ";
					Query query = session.createQuery(hql2);
					query.setString("type", templatetype);
					query.setString("id", hczwtzpz.getId());
					query.executeUpdate();
				}

				return true;
			}

		});

	}
}
